package com.ctrl.riveradministrator.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/4/5.
 */
public class TimeUtil {
    //把字符串转为日期
    public static Date ConverToDate(String strDate) throws Exception
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.parse(strDate);
    }
    public static Calendar getCalendar(Date date){
        // Date转为Calendar
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return  calendar;
    }
    //根据日期获得周几
    public static String getWeek(String pTime) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "日";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }
        return "星期"+Week;
    }
    /**
     * 计算date之前n天的日期
     */
    public static Date getDateBefore(Date date, int n) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - n);
        return now.getTime();
    }
    /**
     * 得到几天后的时间
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 判断给定字符串时间是否为今日
     * @param sdate
     * @return boolean
     */
    public static boolean isToday(String sdate){
        boolean b = false;
        Date time = toDate(sdate);
        Date today = new Date();
        if(time != null){
            String nowDate = dateFormater2.get().format(today);
            String timeDate = dateFormater2.get().format(time);
            if(nowDate.equals(timeDate)){
                b = true;
            }
        }
        return b;
    }
    /**
     * 将字符串转位日期类型
     * @param sdate
     * @return
     */
    public static Date toDate(String sdate) {
        try {
            return dateFormater.get().parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
        return format1.format(date);
    }
    public static String getTime1(Date date) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(date);
    }

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    public static String getNowAccurateTime(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String    date    =    sDateFormat.format(new    Date());
        return  date;
    }
    public static String getNowDate(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        String    date    =    sDateFormat.format(new    Date());
        return  date;
    }
    public static String getNowMonth(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM");
        String    date    =    sDateFormat.format(new    Date());
        return  date;
    }
    public static String getFirstWeekDate(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, 1);
        String    date    =    sDateFormat.format( cal.getTime());
        return  date;
    }
    public static String getFirstMonthDate(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String    date    =    sDateFormat.format( cal.getTime());
        return  date;
    }
    /**
     * 当前季度的开始时间，即2012-01-1 00:00:00
     *
     * @return
     */
    public static  String getFirstQuarterDate() {
        String    date="";
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
//        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            date=  sDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 当前季度的结束时间，即2012-03-30 00:00:00
     *
     * @return
     */
    public static  String getLastQuarterDate() {
        String    date="";
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
//        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3){
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            }
            else if (currentMonth >= 4 && currentMonth <= 6){
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            }

            else if (currentMonth >= 7 && currentMonth <= 9){
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            }
            else if (currentMonth >= 10 && currentMonth <= 12){
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }

            date=  sDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    public static String getFirstYearDate(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR, 1);
        String    date    =    sDateFormat.format(cal.getTime());
        return  date;
    }
    public static String getLastYearDate(){
        SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH,11);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        String    date    =    sDateFormat.format(cal.getTime());
        return  date;
    }

    //获取上月
    public static  String getLastMonth(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.MONTH, -1); //月份份减1
        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        return sf.format(lastMonth);
    }
    public static  Date getLastMonthDate(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.MONTH, -1); //月份份减1
        Date lastMonth = ca.getTime(); //结果
        return lastMonth;
    }

    //获取昨天
    public static  String getLastDayTime(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DAY_OF_YEAR, -1);
        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(lastMonth);
    }
    //获取当前时间
    public static  String getNowDayTime(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
//        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DAY_OF_YEAR, -1); //月份份减1
//        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(new Date ());
    }
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = 0;
        try{
           lt = new Long(s);
        }catch (Exception e){}

        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    public static String stampToDate3(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    public static String stampToDate2(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
}
